package com.sl.au.controller;

import java.util.HashMap;
import java.util.Map;
import java.util.List;

import javax.servlet.http.HttpServletRequest;

import org.joda.time.DateTime;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;
import org.springframework.web.servlet.ModelAndView;

import com.sl.au.entity.BusinesssellerBaseInfo;
import com.sl.au.entity.Evaluate;
import com.sl.au.entity.KitchenWaste;
import com.sl.au.entity.Option;
import com.sl.au.entity.BussinessAccusation;
import com.sl.au.entity.BussinessContent;
import com.sl.au.entity.BussinessInspection;
import com.sl.au.entity.BussinessLevel;
import com.sl.au.service.EvaluateService;
import com.sl.au.service.KitchenWasteService;
import com.sl.au.service.BusinesssellerBaseInfoService;
import com.sl.au.service.BussinessAccusationService;
import com.sl.au.service.BussinessContentService;
import com.sl.au.service.BussinessInspectionService;
import com.sl.au.service.OptionService;
import com.sl.au.service.DivisionService;
import com.sl.utils.MsgHelper;

@RestController
@RequestMapping(value = "/ui/bussinessmain")
public class BusinesssellerBaseInfoController extends BaseController {

	@Autowired
	private BusinesssellerBaseInfoService businesssellerBaseInfoService;
	@Autowired
	private EvaluateService evaluateService;
	@Autowired
	private BussinessAccusationService bussinessAccusationService;
	@Autowired
	private OptionService optionService;
	@Autowired
	private BussinessContentService bussinessContentService;

	@Autowired
	private BussinessInspectionService bussinessInspectionService;
	@Autowired
	private KitchenWasteService kitchenWasteService;
	@Autowired
	private DivisionService divisionService;

	@RequestMapping("/listPartwastes/{bussinessId}")
	public MsgHelper listPartWastes(@PathVariable("bussinessId") String bussinessId) {
		List<KitchenWaste> wastes = kitchenWasteService.listPartwastes(bussinessId);
		return MsgHelper.success().put("wastes", wastes);
	}

	@RequestMapping("/listPartAccusations/{bussinessId}")
	public MsgHelper listPartAccusations(@PathVariable("bussinessId") String bussinessId) {
		List<BussinessAccusation> accusations = bussinessAccusationService.getAccusations(0, 30, bussinessId, null,
				null);
		return MsgHelper.success().put("accusations", accusations);
	}

	@RequestMapping("/listPartInspections/{bussinessId}")
	public MsgHelper listPartInspections(@PathVariable("bussinessId") String bussinessId) {
		List<BussinessInspection> inspections = bussinessInspectionService.getInspections(0, 30, bussinessId, null,
				null);
		return MsgHelper.success().put("inspections", inspections);
	}

	@RequestMapping(method = RequestMethod.GET)
	public ModelAndView index(ModelAndView modelAndView, HttpServletRequest request) {

		String device = request.getParameter("device");
		String identify = request.getParameter("identify");

		/*
		 * String displayEdit = "none"; String displayCheck = "none";
		 * 
		 * String displayInspection = "none"; String displayKitchen = "none";
		 * 
		 * String editURL = "/ui/bussinessEdit"; String checkURL =
		 * "/ui/bussinessCheck"; String inspectionURL ="ui/bussinessInspection";
		 * String kitchenURL ="/ui/kitchenwaste";
		 */

		if (StringUtils.hasText(device) && device.equals("mobile")) {
			List<Option> accusationTypes = optionService.getRepository().findByGroup("举报类型");
			modelAndView.setViewName("mobile/mobileIndex");
			modelAndView.addObject("identify", identify);
			modelAndView.addObject("accusationTypes", accusationTypes);

			/*
			 * for (Module m : this.getMyAccount().getRole().getModules()) { if
			 * (editURL.contains(m.getTarget())) { displayEdit = "display"; } if
			 * (!m.getTarget().equals("#")) { String model =
			 * m.getTarget().split("/")[2]; if (editURL.contains(model))
			 * displayEdit = "display"; }
			 * 
			 * if (checkURL.contains(m.getTarget())) { displayCheck = "display";
			 * } if (!m.getTarget().equals("#")) { String model =
			 * m.getTarget().split("/")[2]; if (checkURL.contains(model))
			 * displayCheck = "display"; }
			 * 
			 * 
			 * if (inspectionURL.contains(m.getTarget())) { displayInspection =
			 * "display"; } if (!m.getTarget().equals("#")) { String model =
			 * m.getTarget().split("/")[2]; if (inspectionURL.contains(model))
			 * displayInspection = "display"; } if
			 * (kitchenURL.contains(m.getTarget())) { displayKitchen =
			 * "display"; } if (!m.getTarget().equals("#")) { String model =
			 * m.getTarget().split("/")[2]; if (kitchenURL.contains(model))
			 * displayKitchen = "display"; } } BusinesssellerBaseInfo
			 * businesssellerBaseInfo =
			 * this.businesssellerBaseInfoService.getRepository()
			 * .getInfoByUserId(this.getMyAccount().getUser().getId()); if
			 * (!businesssellerBaseInfo.getId().equals(identify)) { displayEdit
			 * = "none"; } modelAndView.addObject("displayEdit", displayEdit);
			 * modelAndView.addObject("displayCheck", displayCheck);
			 * modelAndView.addObject("displayInspection", displayInspection);
			 * modelAndView.addObject("displayKitchen", displayKitchen);
			 */
		}

		return modelAndView;
	}

	@RequestMapping(value = "getBussinesserInfo/{identify}", method = RequestMethod.GET)
	public Map<String, Object> getBussinesserInfo(@PathVariable String identify) {
		Map<String, Object> map = new HashMap<String, Object>();
		BusinesssellerBaseInfo businesssellerBaseInfo = this.businesssellerBaseInfoService.getRepository()
				.findOne(identify);
		map.put("businesssellerBaseInfo", businesssellerBaseInfo);
		Evaluate evaluate = evaluateService.getRepository().getInfoByBusinessseller(businesssellerBaseInfo.getId());
		map.put("evaluate", evaluate);
		map.put("modules", this.getMyAccount().getRole().getModules());
		/*
		 * map.put("recordsFiltered", count); map.put("recordsTotal", count);
		 */
		return map;
	}

	@RequestMapping(value = "saveBusinessAccusation", method = RequestMethod.POST)
	public Map<String, Object> saveBusinessAccusation(@RequestBody BussinessAccusation bussinessAccusation) {
		Map<String, Object> map = new HashMap<String, Object>();
		int code = -1;
		bussinessAccusation.setAccusationDate(DateTime.now().toDate());
		bussinessAccusation.setUser(this.getMyAccount().getUser());
		List<Option> options = this.optionService.getRepository().findByGroupAndTitle("投诉建议状态", "未受理");
		if (options.size() > 0)
			bussinessAccusation.setInfoState(options.get(0));

		this.bussinessAccusationService.getRepository().save(bussinessAccusation);
		code = 1;
		map.put("code", code);
		return map;
	}

	@RequestMapping(value = "saveBusinessAccusationWithFile", method = RequestMethod.POST)
	public Map<String, Object> saveBusinessAccusationWithFile(BussinessAccusation bussinessAccusation,
			@RequestParam("file") MultipartFile file) {
		Map<String, Object> map = new HashMap<String, Object>();
		int code = -1;
		bussinessAccusation.setAccusationDate(DateTime.now().toDate());
		bussinessAccusation.setUser(this.getMyAccount().getUser());
		List<Option> options = this.optionService.getRepository().findByGroupAndTitle("投诉建议状态", "未受理");
		if (options.size() > 0)
			bussinessAccusation.setInfoState(options.get(0));
		if (file.getOriginalFilename() != null && !"".equals(file.getOriginalFilename())) {

			String url = getUploadFilePath(this.fileUploadPath, "img/accusation", file);
			bussinessAccusation.setPictureAddr(url);
		}
		this.bussinessAccusationService.getRepository().save(bussinessAccusation);
		code = 1;
		map.put("code", code);
		return map;
	}

	@RequestMapping(value = "getBussinesserContent/{identify}", method = RequestMethod.GET)
	public MsgHelper getBussinesserContent(@PathVariable String identify) {
		MsgHelper msgHelper = null;
		BusinesssellerBaseInfo businesssellerBaseInfo = this.businesssellerBaseInfoService.getRepository()
				.findOne(identify);
		if (businesssellerBaseInfo != null) {
			BussinessLevel bussinessLevel = businesssellerBaseInfo.getBussinessLevel();
			if (bussinessLevel != null) {
				BussinessContent bussinessContent = bussinessContentService.getRepository().findByDivision(this.divisionService.getRepository().findOne(bussinessLevel.getId()));
				msgHelper = MsgHelper.success();
				Map<String, Object> data = new HashMap<String, Object>();
				data.put("BussinessContent", bussinessContent);
				String content = bussinessContent.getBussinessContent();
				if (content == null || content.equals("")) {
					msgHelper.setCode(0);
				}
				msgHelper.setData(data);
			}
		}
		if (msgHelper == null) {
			msgHelper = MsgHelper.failed();
		}
		return msgHelper;
	}

	@RequestMapping("/findById/{id}")
	public MsgHelper findById(@PathVariable("id") String id) {
		BussinessInspection info = bussinessInspectionService.findById(id);
		return MsgHelper.success().put("info", info);
	}
}
